package com.thl.filechooser;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.util.Util;

import java.io.File;

/**
 * Created by Tony on 2018/1/16.
 */

public class FileChooserGlide {

    public static void display(Context context, ImageView imageView, String url, int placeholder, int error) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url).placeholder(placeholder)
                .error(error).into(imageView);
    }

    public static void display(ImageView imageView, String url) {
        if (imageView == null) {
            return;
        }
        Glide.with(imageView.getContext()).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
//                .placeholder(R.drawable.ic_image_loading)
                .dontAnimate()
                .into(imageView);
    }
    // 使用Glide加载图片并添加圆角效果
    @SuppressLint("CheckResult")
    public static void displayCorner(ImageView imageView, String imageUrl, int cornerRadius) {
//        RequestOptions requestOptions = new RequestOptions();
//        requestOptions.transform(new RoundedCorners(cornerRadius));
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.transforms(new CenterCrop(), new RoundedCorners((int) cornerRadius));

        Glide.with(imageView.getContext())
                .load(imageUrl)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .dontAnimate()
                .apply(requestOptions)
                .into(imageView);
    }


    public static void displayInside(Context context, ImageView imageView, String url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerInside()
//                .placeholder(R.drawable.ic_image_loading)
                .dontAnimate()
                .into(imageView);
    }

    public static void displayImageTouch(Context context, ImageView imageView, String url, int resizeX,int resizeY) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .placeholder(R.drawable.ic_image_loading)
                .apply(new RequestOptions()
                        .override(resizeX, resizeY)
                        .priority(Priority.HIGH)
                        .fitCenter())
                .dontAnimate()
                .into(imageView);
    }
    public static void displayGif(Context context, ImageView imageView, String url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context)
                .asGif()
                .load(url)
//                .apply(new RequestOptions()
//                        .override(resizeX, resizeY)
//                        .priority(Priority.HIGH)
//                        .fitCenter())
                .into(imageView);
    }

    public static void displayBitmap(Context context, ImageView imageView, Bitmap bitmap) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(bitmap)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
//                .placeholder(R.drawable.ic_image_loading)
                .dontAnimate()
                .into(imageView);
    }

    public static void displayAlbum(Context context, ImageView imageView, String url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
//                .placeholder(R.drawable.ic_image_loading)
                .dontAnimate()
                .into(imageView);
    }

    public static void displayFit(Context context, ImageView imageView, String url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .fitCenter()
//                .placeholder(R.drawable.ic_image_loading)
                .dontAnimate()
                .into(imageView);
    }

    public static void displayFitCenter(Context context, ImageView imageView, String url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .fitCenter()
//                .placeholder(R.drawable.ic_image_loading)
                .into(imageView);
    }

    public static void display(Context context, ImageView imageView, File url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
//                .placeholder(R.drawable.ic_image_loading)
                .into(imageView);
    }

    public static void displaySmallPhoto(Context context, ImageView imageView, String url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
//                .placeholder(R.drawable.ic_image_loading)
                .thumbnail(0.5f)
                .into(imageView);
    }

    public static void displayBigPhoto(Context context, ImageView imageView, String url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .format(DecodeFormat.PREFER_ARGB_8888)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void display(Context context, ImageView imageView, int url) {
        if (imageView == null || null == context) {
            throw new IllegalArgumentException("argument error");
        }
        Glide.with(context).load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
//                .placeholder(R.drawable.ic_image_loading)
                .into(imageView);
    }

    public static void displayVideo(Context context, ImageView imageView, String videoPath) {
        if (imageView == null || null == context) {
            return;
        }
        try {
            /*Rx.runSub(new RxCall<Bitmap>() {
                @Override
                public Bitmap doSub() {
                    MediaMetadataRetriever media = new MediaMetadataRetriever();
                    media.setDataSource(videoPath);
                    return media.getFrameAtTime();
                }

                @Override
                public void onUi(Bitmap bitmap) {
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setImageBitmap(bitmap);
                }
            });*/
            Glide.with(context)
                    .asBitmap()
                    .load(videoPath)
                    .apply(new RequestOptions().centerCrop().frame(0))
                    .into(imageView);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void displayVideoCursor(Context context, ImageView imageView, String videoPath) {
        Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Video.Media.ALBUM + "=?", new String[]{videoPath}, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
        try {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                //视频缩略图路径
                String albumPath = "";
                Cursor thumbCursor = context.getApplicationContext().getContentResolver().query(
                        MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                        null, MediaStore.Video.Thumbnails.VIDEO_ID
                                + "=" + id, null, null);
                if (thumbCursor.moveToFirst()) {
                    albumPath = thumbCursor.getString(thumbCursor
                            .getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                    Bitmap bitmap = BitmapFactory.decodeFile(albumPath);
                    imageView.setImageBitmap(bitmap);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }


    /**
     * 暂停所有的加载图片任务
     */
    public static void pauseRequestsRecursive(Activity activity) {
        Glide.with(activity).pauseRequestsRecursive();
    }


    /**
     * 恢复所有的加载图片任务
     */
    public static void resumeRequestsRecursive(Activity activity) {
        Glide.with(activity).resumeRequestsRecursive();
    }

    /**
     * 相应的销毁方法
     */
    public static void onDestory(Context context) {
        if (Util.isOnMainThread()) {
            Glide.with(context).onDestroy();
        }
    }

    public interface loadDownListener {
        void loadSuc();
        void loadFail();
    }
}
